﻿using System;
using System.Collections.Generic;
using System.Web;


namespace CCPSIP.Group
{
    /// <summary>取得群組訊息已讀/未讀清單(誰已讀)</summary>

    public class who_read : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest req = context.Request;
            HttpResponse resp = context.Response;
            resp.ContentType = "text/plain";
            resp.ContentEncoding = System.Text.Encoding.UTF8;
            DateTime reqTime = DateTime.Now;
            string resultStr = "";


            try
            {
                //【01】取得POST參數，驗證身分
                //
                string idHeader = req.Form["idHeader"];
                string checkcode = req.Form["checkcode"];
                string MsgID = req.Form["MsgID"];      //Server端的群組訊息ID
                string ClientGID = req.Form["GroupID"];//Client端的群組ID
                string LocalPIDStr = req.Form["LocalPID"];//Client端的群組訊息ID

                SipUserInfo userInfo = SipKit.GetUserInfo(idHeader, checkcode, req);
                if (userInfo == null)
                {
                    throw new Exception("Error_01: Cannot identify this device");//無法識別此裝置
                }


                //【02】檢查群組訊息是否存在
                //
                long GMsgID = -1;
                bool hadGroupMsg = false;

                try
                {
                    if (!string.IsNullOrEmpty(MsgID))
                    {
                        GMsgID = Convert.ToInt64(MsgID);
                    }
                    else
                    {
                        int LocalPID = Convert.ToInt32(LocalPIDStr);
                        GMsgID = OracleKitMsg.getGroupMsgID(ClientGID, userInfo.SIP_Num, LocalPID);
                    }

                    if (GMsgID != -1)
                        hadGroupMsg = OracleKitMsg.hadGroupMsg(GMsgID, userInfo.SIP_Num);
                }
                catch
                {
                    throw new Exception("Error_02: Parameter Error");//參數錯誤
                }

                if (!hadGroupMsg)
                {
                    throw new Exception("Error_03: Message not found");//找不到此訊息
                }


                //【03】取得已讀/未讀清單資訊
                //
                Dictionary<string, string>[] resultArray = OracleKitMsg.getGroupReadInfo(GMsgID);


                //【99】回傳結果
                //
                resp.ContentType = "application/json";
                resp.Charset = null;
                fastJSON.JSON.Instance.Parameters.EnableAnonymousTypes = true;
                resultStr = fastJSON.JSON.Instance.ToJSON(resultArray);
            }
            catch (Exception ex)
            {
                resp.ContentType = "application/xml";
                resultStr = ChrisKit.Ex2Xml(ex, false);
            }
            finally
            {
                int contentLength = resp.ContentEncoding.GetByteCount(resultStr);
                resp.AppendHeader("Content-Length", contentLength.ToString());
                resp.Write(resultStr);
                resp.Flush();
                resp.Close();
            }
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
